import tkinter as tk
from tkinter import ttk
from dao.Result_leave_history import Result
from middleWare import Middleware
from tkinter import filedialog
import pandas as pd
from tkinter import messagebox

class AttendanceDetailsApp(tk.Frame):
    def __init__(self,root):
        self.root = root
        self.root.title("考勤详情")
        self.root.iconbitmap("static/favicon.ico")
        self.gethis()
        self.setup_window()
        self.create_widgets()
        

    def setup_window(self):
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()

        window_width = 600
        window_height = 400
        x_position = (screen_width - window_width) // 2
        y_position = (screen_height - window_height) // 2

        self.root.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")

    def create_widgets(self):
        # 创建滚动条
        scrollbar = tk.Scrollbar(self.root, orient="vertical")

        # 创建Canvas
        canvas = tk.Canvas(self.root, yscrollcommand=scrollbar.set, width=400, height=400)
        canvas.place(relx=0, rely=0, relwidth=1, relheight=0.9)

        # 创建表格头部
        header = ["时间", "班级", "教师", "缺勤名单"]

        # 创建表格
        self.table = ttk.Treeview(canvas, columns=header, show="headings")

        # 配置表格列
        for col in header:
            self.table.heading(col, text=col)
            self.table.column(col, width=100, anchor="center")


        for row in range(len(self.dic['时间'])):
            self.table.insert("", "end", values=(self.dic['时间'][row],self.dic['班级'][row],self.dic['教师'][row],self.dic['缺勤名单'][row]))

        # 放置表格
        self.table.place(relx=0, rely=0, relwidth=1, relheight=1)

        # 配置滚动条
        scrollbar.config(command=self.table.yview)
        scrollbar.place(relx=1, rely=0, relheight=1, anchor="ne")

        # 创建导出按钮
        export_button = tk.Button(self.root, text="导出", command=self.export_data, bg="#3b80f9", fg="white", width=10, height=1)
        export_button.place(relx=0.5, rely=0.98, anchor="s")

    def gethis(self):
        mid = Middleware()
        self.dic = mid.data_bind_his()
        for i in self.dic:
            self.dic[i].reverse()


    def export_data(self):
        # 在这里添加导出数据的功能，根据你的实际需求
        directory = filedialog.askdirectory()
        if directory:
            df = pd.DataFrame(self.dic)
            df.to_excel(directory+"/考勤记录.xlsx")
            messagebox.showinfo("成功","导出成功!")
            
            

    def run(self):
        # 运行主循环
        self.root.mainloop()